<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .name{
            width: 100px;
            height: 50px;
            border: 2px red solid;
            line-height: 50px;
        }
    </style>
</head>
<body>
    <div class="name">你好1</div>
    <div class="name">你好2</div>
    <div class="name">你好3</div>
    <div class="name">你好4</div>
    <div class="name">你好5</div>
    <script>
        var dom=document.getElementsByClassName("name");
        // for (var i=0 ;i<dom.length; i++) {
        //     dom[i].onclick=function(){
        //         console.log(i);
        //     }
        // }
        for (let i =0 ;i < dom.length; i++) {
            dom[i].onclick=function(){
                console.log(i);
                console.log(dom[i].innerText);
            }
        }
        //垃圾清除  标记清楚 技术清除
    </script>
</body>
</html>